package com.itheima02.dao;

import com.itheima02.domain.Student;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class StudentDao {

    public List findAll() {
        List list = new ArrayList();
        Connection con = null;
        Statement stat = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3307/db1", "root", "root");
            String sql = "SELECT *FROM student";
            stat = con.createStatement();
            rs = stat.executeQuery(sql);
            while (rs.next()) {
                Integer id = rs.getInt("id");
                String name = rs.getString("name");
                Integer age = rs.getInt("age");
                Date birthday = rs.getDate("birthday");
                Student stu = new Student();
                stu.setId(id);
                stu.setName(name);
                stu.setAge(age);
                stu.setBirthday(birthday);
                list.add(stu);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (stat != null) {
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }

    public void insert(Student stu) {
        Connection con = null;
        PreparedStatement stat = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3307/db1", "root", "root");
            stat = con.prepareStatement("insert into student values (null,?,?,?)");
            stat.setString(1, stu.getName());
            stat.setInt(2, stu.getAge());


            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String format = simpleDateFormat.format(stu.getBirthday());

            stat.setString(3, format);
            int i = stat.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (stat != null)
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }

        }


    }

    public void delete(String id) {
        Connection con = null;
        PreparedStatement statement = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3307/db1", "root", "root");
            statement = con.prepareStatement("delete from student where id=?");
            statement.setString(1, id);
            statement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void update(Student stu) {}
    public  void findById(String id ){}
}
